package org.example;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FunctionLayer {
    // 数据库连接信息
    private final String url = "jdbc:mysql://localhost:3306/contact"; // 设置数据库URL，包括数据库名称
    private final String username = "root";
    private final String password = "314489";
    private Connection connection;

    public FunctionLayer(){
//        连接数据库
        connect();
    }



    // 连接数据库
    private boolean connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("成功连接到数据库");
            return true;
        } catch (ClassNotFoundException e) {
            System.err.println("JDBC 驱动程序类未找到：" + e.getMessage());
            return false;
        } catch (SQLException e) {
            System.err.println("SQL 连接错误：" + e.getMessage());
            return false;
        }
    }



    // 添加联系人的方法
    public boolean addContact(String name, String phone, String address) {
        // 检查输入框是否为空
        if (name == null || name.trim().isEmpty() ||
                phone == null || phone.trim().isEmpty() ||
                address == null || address.trim().isEmpty()) {
            return false; // 添加失败
        }

//        在数据库中执行SQL语句
        String query = "INSERT INTO contacts (name, phone, address) VALUES (?, ?, ?)";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, name);
            statement.setString(2, phone);
            statement.setString(3, address);
            statement.executeUpdate();
            return true; // 添加成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 添加失败
        }
    }

    // 删除联系人的方法
    public boolean deleteContact(String id) {
//        在数据库中执行SQL语句
        String query = "DELETE FROM contacts WHERE id = ?";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, id);
            statement.executeUpdate();
            return true; // 删除成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 删除失败
        }
    }



    // 获取联系人的列表
    public List<String[]> getContactList() {
//        执行SQL语句
        List<String[]> contacts = new ArrayList<>();
        String query = "SELECT id, name, phone, address FROM contacts"; // 添加ID字段
        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(query)) {
            while (resultSet.next()) {
                String id = String.valueOf(resultSet.getInt("id")); // 获取ID
                String name = resultSet.getString("name");
                String phone = resultSet.getString("phone");
                String address = resultSet.getString("address");
                contacts.add(new String[]{id, name, phone, address}); // 添加ID到联系人数组
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return contacts;
    }

    // 修改联系人的方法
    public boolean editContact(String id, String name, String phone, String address) {
        System.out.println(id);
        System.out.println(name);
        System.out.println(phone);
        System.out.println(address);
        // 检查输入框是否为空
        if (name == null || name.trim().isEmpty() ||
                phone == null || phone.trim().isEmpty() ||
                address == null || address.trim().isEmpty()) {
            System.out.println("有空");
            return false; // 修改失败
        }

        // 在数据库中执行SQL语句
        String query = "UPDATE contacts SET name = ?, phone = ?, address = ? WHERE id = ?";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, name);
            statement.setString(2, phone);
            statement.setString(3, address);
            statement.setString(4, id);
            statement.executeUpdate();
            return true; // 修改成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 修改失败
        }
    }
}
